package com.aaa.book_mall.controller;

import com.aaa.book_mall.dao.BookDao;
import com.aaa.book_mall.dao.OutAuditDao;
import com.aaa.book_mall.dao.OutDao;
import com.aaa.book_mall.dao.OutDetailDao;
import com.aaa.book_mall.entity.*;
import com.aaa.book_mall.util.JDBCUtil;
import com.aaa.book_mall.util.LayTab;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: 操作出库表
 * @ClassName: OutController
 * @Author: fengyun
 * @Date: 2021/1/20 10:48
 * @Version: 1.0
 */
@RestController
@RequestMapping("/store")
public class OutController {
    @Autowired
    private OutAuditDao outAuditDaoimpl;

    @Autowired
    private BookDao bookDaoimpl;

    @Autowired
    private OutDetailDao outDetailDaoimpl;

    @Autowired
    private OutDao outDaoimpl;

    /* *
     *  向t_out、t_outdetail表中添加数据
     * @param bookdata
     * @param request
     * @return java.util.Map
     * @date 2021/1/20 19:05
     */
    @RequestMapping("/out")
    public Map out(@RequestBody ArrayList<Book> bookdata, HttpServletRequest request){
        System.out.println("out....................");
//      通过session获取登录人的信息，用以生成outnumber
        HttpSession session = request.getSession();
        Staff staff =(Staff) session.getAttribute("staff");
//      获取session中的员工编号
        int staffId=staff.getStaffId();
//        获取session中的员工姓名
        String staffname = staff.getStaffName();
        System.out.println("提交申请staffId"+staffId);
//        获取现在的时间，和用户便函共同组成outnumber
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        String format = simpleDateFormat.format(date);
        String stringDate =staffId+format.replace("-", "").replace(":","");


        System.out.println("outnumber"+stringDate);
//        计算该次订单共又多少本书，共多少钱
        int bookcount=0;
        double bookprice=0;
//      循环遍历前台输入的数据
        if(bookdata.size()>0){
            for (Book bookdatum : bookdata) {
                OutDetail outDetail = new OutDetail(null, stringDate, bookdatum.getBookid(), bookdatum.getCount());
//               向out_detail表中加入数据
                outDetailDaoimpl.insertOutDetail(outDetail);
                System.out.println("...................................");
//                计算累计多少本书，累计多少钱
                bookcount+=bookdatum.getCount();
                bookprice+=(bookdatum.getCount()*bookdatum.getPrice());
            }
        }
        System.out.println(bookcount+":"+bookprice);
        Out out = new Out(null, stringDate, staffId, staffname.toString(), bookcount, bookprice, date, "0");
//       向t_out表中写数据
        int i = outDaoimpl.intserOut(out);

        HashMap<Object, Object> map = new HashMap<>();
        if(i>0){
            map.put("code","1");
            map.put("msg","添加成功");
        }else{
            map.put("code","0");
            map.put("msg","添加 失败");
        }
        return map;
    }

    /* *
     *   查询所有没有审核的数据
     * @return com.aaa.book_mall.util.LayTab
     * @date 2021/1/20 19:05
     */
    @RequestMapping("/showNoAudit")
    public LayTab noAudit(Out out){
        System.out.println(out);
//        查询所有没有审核的数据
        List<Map<String, Object>> maps = outDaoimpl.queryNoAuditInfo(out);
        System.out.println(maps);
//        查询没有审核数据的总数
        int countInfo = outDaoimpl.queryCountInfo();
        System.out.println(countInfo);
        LayTab layTab = new LayTab(countInfo, maps);
        System.out.println(layTab);
        return layTab;
    }

    /* *
     *  审核通过时得操作
     * @param outs
     * @param request
     * @return java.util.Map
     * @date 2021/1/21 21:02
     */
    @RequestMapping("/agree")
    public Map agree(@RequestBody Out outs,HttpServletRequest request){
        System.out.println(outs);
        String status="1";
//        1、更改t_out表中的状态
        int statusV = outDaoimpl.updateStatus(status, outs.getOutnumber());
/*       2、更爱t_book对应书本的数量
*             先根据outnumber到t_outdetail表中查询申请的每一本书
*             根据查询出来的书的量和书的编号到t_book表中操作
* */
        List<OutDetail> outDetails = outDetailDaoimpl.queryLOD(outs.getOutnumber());//查询订单详情
        if(outDetails!=null && outDetails.size()>0){
            for (OutDetail outDetail : outDetails) {
                int bookcount = bookDaoimpl.selectCountByBookid(outDetail.getBookid());//查询t_book中的书本数量
                bookcount=bookcount-outDetail.getBookcount();//将原有数量减去申请的数量
                bookDaoimpl.updateCountByBookid(bookcount,outDetail.getBookid());//更新t_book中的count
            }
        }
//        3、写入到t_audit表
//      通过session获取登录人的信息，用以生成outnumber
        HttpSession session = request.getSession();
        Staff staff =(Staff) session.getAttribute("staff");
//      获取session中的员工编号
        int staffId=staff.getStaffId();
//        获取session中的员工姓名
        String staffname = staff.getStaffName();
        System.out.println("审核人员的staffId"+staffId);
//        获取现在的时间，和用户bianhao共同组成outnumber
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        String format = simpleDateFormat.format(date);
        String stringDate =staffId+format.replace("-", "").replace(":","");

        OutAudit outAudit = new OutAudit(null, stringDate, staffId, staffname.toString(), outs.getOutnumber(), "同意", "同意", date);
        int i = outAuditDaoimpl.insertInfo(outAudit);

        HashMap<Object, Object> map = new HashMap<>();
        if(i>0){
            map.put("code","1");
            map.put("msg","操作成功");
        }else{
            map.put("code","0");
            map.put("msg","操作失败");
        }
        return map;
    }

    /* *
     *   查询所有t_out的信息
     * @return com.aaa.book_mall.util.LayTab
     * @date 2021/1/21 21:08
     */
    @RequestMapping("/getAllOutInfo")
    public LayTab getAllOutInfo(Out out){
        System.out.println(out);
//        查询出来目标数据
        List<Map<String, Object>> allOutInfo = outDaoimpl.getAllOutInfo(out);
//        查询出来所有的总条数
        int count = outDaoimpl.queryCountAllInfo();
        LayTab layTab = new LayTab(count, allOutInfo);
        return layTab;
    }

    /**
     * 查询出库商品数量
     * @param out
     * @return
     */
    @RequestMapping("/selectOut")
    public Map selectOut(Out out){
        System.out.println("进入查询出库商品数量。。");
        List<Map<String, Object>> list = outDaoimpl.selectOut(out);
        System.out.println(list);
        HashMap<Object, Object> map = new HashMap<>();

        map.put(list.get(0),list.get(1));
        System.out.println("出库信息："+map);
        return map;

    }

    @RequestMapping("/selectCountGroup")
    public List<Map<String, Object>> selectCountGroup(){
        List<Map<String, Object>> maps = outDaoimpl.selectSellCount();
        return maps;
    }

    @RequestMapping("/selectPublisher")
    public List<Map<String, Object>> selectPublisher(){
        System.out.println("进入查询出版社销售排行controller。。");
        List<Map<String, Object>> maps = outDaoimpl.selectPublisher();
        return maps;
    }
}
